/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "gtest-qt.h"
#include "chanwatcher.h"
#include "mockobjects/mockchanmanager.h"
#include "mockobjects/mocktimer.h"

namespace
{
	using namespace Wishmaster;
	using ::testing::_;

	class SutChanWatcher : public ChanWatcher
	{
	public:
		MockObjects::MockTimer* lastCreatedTimer;
	protected:
		virtual QTimer* createTimer(QObject* parent = 0)
		{
			lastCreatedTimer = new MockObjects::MockTimer(parent);
			return lastCreatedTimer;
		}
	};

	class TestChanWatcher : public ::testing::Test
	{
	public:
		virtual void SetUp()
		{
			watcher = new SutChanWatcher();
			manager = new MockObjects::MockChanManager();
			watcher->setManager(manager);
		}

		virtual void TearDown()
		{
			delete manager;
			delete watcher;
		}

		MockObjects::MockChanManager* manager;
		SutChanWatcher* watcher;
	};

	TEST_F(TestChanWatcher, setBoardWatchParameters)
	{
		BoardWatchParameters params;
		params.chanName = "testchan.ru";
		params.boardId = "/b/";
		params.refresh = true;
		params.onlyIndex = true;
		params.refreshPeriod = 60;
		watcher->setBoardWatchParameters(params);

		BoardWatchParameters obtainedParams = watcher->boardWatchParameters("testchan.ru", "/b/");

		ASSERT_EQ(params.chanName, obtainedParams.chanName);
		ASSERT_EQ(params.boardId, obtainedParams.boardId);
		ASSERT_EQ(params.refresh, obtainedParams.refresh);
		// Whatever
	}

	TEST_F(TestChanWatcher, removeBoardWatchParameters)
	{
		BoardWatchParameters params;
		params.chanName = "testchan.ru";
		params.boardId = "/b/";
		params.refresh = true;
		params.onlyIndex = true;
		params.refreshPeriod = 60;
		watcher->setBoardWatchParameters(params);
		watcher->removeBoardWatchParameters(params.chanName, params.boardId);

		BoardWatchParameters obtainedParams = watcher->boardWatchParameters("testchan.ru", "/b/");

		EXPECT_TRUE(obtainedParams.chanName.isNull());
		EXPECT_TRUE(obtainedParams.boardId.isNull());
	}

	TEST_F(TestChanWatcher, onTimerTimeout_requestsBoard)
	{
		BoardWatchParameters params;
		params.chanName = "testchan.ru";
		params.boardId = "/b/";
		params.refresh = true;
		params.onlyIndex = true;
		params.refreshPeriod = 60;
		watcher->setBoardWatchParameters(params);
		EXPECT_CALL(*manager, requestBoardIndex(QString("testchan.ru"), QString("/b/"), _));

		EXPECT_TRUE(watcher->lastCreatedTimer != 0);
		watcher->lastCreatedTimer->provokeTimeout();
	}

	TEST_F(TestChanWatcher, onTimerTimeout_refreshIsDisableds_doesntCreateTimer)
	{
		BoardWatchParameters params;
		params.chanName = "testchan.ru";
		params.boardId = "/b/";
		params.refresh = false;
		params.onlyIndex = true;
		params.refreshPeriod = 60;
		watcher->setBoardWatchParameters(params);
		EXPECT_CALL(*manager, requestBoardIndex(QString("testchan.ru"), QString("/b/"), _)).Times(0);

		EXPECT_TRUE(watcher->lastCreatedTimer == 0);
	}
}
